<template>
  <div>
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell v-for="item in list" :key="item.com_id">
        <div class="box">
          <div class="left">
            <van-image round fit="cover" :src="item.aut_photo" />
          </div>

          <div class="right">
            <p>{{ item.aut_name }}</p>
            <span>{{ item.content }}</span>
            <div class="btn">
              <span>{{ item.pubdate | relvTime }}</span>
              <van-button @click="replays(item)" class="reply" size="mini"
                >回复({{ item.reply_count }})</van-button
              >
            </div>
          </div>
          <van-icon name="like-o" />
        </div>
      </van-cell>
    </van-list>
    <!-- 发表评论栏 -->
    <van-field
      placeholder
      v-model="sms"
      center
      clearable
      placeholder="请输入评论内容"
    >
      <template #button>
        <van-button @click="send" size="small" type="primary">发布</van-button>
      </template>
    </van-field>
    <!-- 动作面板 -->
    <van-action-sheet v-model="show" title="发表回复">
      <div class="content">
        <re-play v-if="show" :cmt="cmt"></re-play>
      </div>
    </van-action-sheet>
  </div>
</template>

<script>
import { cmtListApi, sendCmtApi } from '@/Api'
import RePlay from './replay.vue'
export default {
  components: {
    're-play': RePlay
  },
  data () {
    return {
      cmt: '',
      show: false,
      sms: '',
      list: [],
      loading: false,
      finished: false,
      offset: null
    }
  },
  methods: {
    replays (item) {
      this.show = true

      this.cmt = item
    },
    async onLoad () {
      let res = await cmtListApi({
        type: 'a',
        source: this.$route.query.article_id,
        offset: this.offset
      })
      // console.log(res)
      //   将获取到的评论id 放到this.offset里面
      this.offset = res.data.data.last_id
      this.list.push(...res.data.data.results)
      this.loading = false
      if (this.offset === res.data.data.end_id) {
        this.finished = true
      }
    },
    // 点击发布按钮事件
    async send () {
      let res = await sendCmtApi({
        target: this.$route.query.article_id,
        content: this.sms
      })
      // console.log('pinglun:', res)
      this.list.unshift(res.data.data.new_obj)
      this.sms = ''
      this.$toast('发布成功')
    }
  }
}
</script>

<style lang="less" scoped>
.van-list {
  margin-bottom: 54px;
}
// 底部搜索栏样式
.van-field {
  position: fixed;
  left: 0;
  bottom: 0;
}
.box {
  width: 100%;
  display: flex;
  align-items: center;

  .van-image {
    width: 50px;
    height: 50px;
  }
  .right {
    flex: 1;
    margin-left: 10px;
    p {
      font-size: 14px;
      color: #6282ac;
    }
    span {
      font-size: 14px;
    }
    .btn {
      display: flex;
      align-items: center;
      span {
        font-size: 14px;
        color: #a8a8a8;
      }
    }
  }
}
</style>
